import { axiosInstance } from '../axios'
import CommonConfig from '../../assets/js/config'
const processUrl = CommonConfig.processUrl + '/v1'

export function productSpecGroup () {
  const url = processUrl + '/product_spec_group/all'
  return axiosInstance.get(url).then((res) => {
    return Promise.resolve((res.data))
  })
}

export function searchProductSpecGroup (keyWords) {
  const url = processUrl + '/product_spec_group/all'
  let reqData = {
    params: {
      keyWords
    }
  }
  return axiosInstance.get(url, reqData).then((res) => {
    return Promise.resolve(res)
  })
}

export function getProductSpec (params) {
  const newUrl = `${processUrl}/product_spec`
  return axiosInstance.get(newUrl, { params }).then((res) => {
    return Promise.resolve(res)
  })
}
export function getProductSpecAll (groupId) {
  const params = {
    params: {
      groupId
    }
  }
  const newUrl = `${processUrl}/product_spec/all`
  return axiosInstance.get(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

export function addEditTypeName (params) {
  const newUrl = `${processUrl}/product_spec_group`
  return axiosInstance.put(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}
// 是否在用校验
export function isUsing (id, isType = false) {
  const params = {
    params: {
      id
    }
  }
  const url = `${processUrl}/${isType ? 'product_spec_group' : 'product_spec'}/is_using`
  return axiosInstance.get(url, params).then((res) => {
    return Promise.resolve((res.data))
  })
}
export function delType (id) {
  const params = {
    params: {
      id
    }
  }
  const newUrl = `${processUrl}/product_spec_group`
  return axiosInstance.delete(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

export function batchAdd (params) {
  const newUrl = `${processUrl}/product_spec/batch`
  return axiosInstance.post(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}
export function delSpec (id) {
  const params = {
    params: {
      id
    }
  }
  const newUrl = `${processUrl}/product_spec`
  return axiosInstance.delete(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

export function editSpec (params) {
  const newUrl = `${processUrl}/product_spec`
  return axiosInstance.patch(newUrl, params).then((res) => {
    return Promise.resolve(res)
  })
}

export function uploadProductSpec (query) {
  const url = `${processUrl}/product_spec/import/${query.groupId}`
  return axiosInstance.post(url, query).then((res) => {
    return Promise.resolve(res)
  })
}
export function productSpecTemp () {
  const url = processUrl + '/product_spec/template'
  return axiosInstance.get(url).then((res) => {
    return Promise.resolve(res)
  })
}
